package com.shuang.controller;

import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baidu.ueditor.ActionEnter;
import com.shuang.common.UploadData;
import com.shuang.service.FTPService;

/**
 * 富文本编辑器核心处理类
 * @author dengshuangjun
 * Created by dengshuangjun on 2016/11/20.
 */
@Controller
@RequestMapping("/ued")
public class UEditorHandler {
	
	@Resource(name="fTPService")
	private FTPService fTPService;
	/**
	 * 编辑器数据配置
	 * @param request
	 * @return
	 */
	 @RequestMapping(value="/config",params="action=config")
	 @ResponseBody
	 public String config(HttpServletRequest request) {
		 String rootPath = request.getSession().getServletContext().getRealPath("/");   
		 String exec = new ActionEnter(request, rootPath).exec();
	     return exec; 
	 }
	/**
	 * 上传图片
	 * @param upfile
	 * @return
	 */
	@RequestMapping(value="/config",params="action=uploadimage")
	@ResponseBody
	public UploadData upImageFile(MultipartFile upfile){
		
		return fTPService.imageUpload(upfile);
	 }
	 
	 /**
	 * 生成含有字母的验证码
	 * @return
	 */
	 public String picSting(){
		Random ra = new Random();
		int num1;
		StringBuffer sbf2 = new StringBuffer();
		while (sbf2.length() < 8) {
			if(ra.nextInt(3)==0){
				num1=ra.nextInt(10);
				sbf2.append(num1);
			}else if(ra.nextInt(3)==1){
				num1=ra.nextInt(26)+97;
				sbf2.append((char)num1);
			}else{
				num1=ra.nextInt(26)+65;
				sbf2.append((char)num1);
			}
		}
		return sbf2.toString();
	}
	/**
	 * 上传视频
	 * @param upfile
	 * @return
	 */
	 @RequestMapping(value="/config",params="action=uploadvideo")
	 @ResponseBody
	 public UploadData uploadvideo(MultipartFile upfile){
		 return fTPService.imageUpload(upfile);
	 }
	 /**
	  * 上传文件
	  * @param upfile
	  * @return
	  */
 @RequestMapping(value="/config",params="action=uploadfile")
	 @ResponseBody
	 public UploadData uploadFile(MultipartFile upfile){return fTPService.imageUpload(upfile);
	 }
}
